package com.wzvtc.vsl.securitytool;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Vsl on 2015/3/15.
 */
public class HomeActivity extends FragmentActivity implements View.OnClickListener{
    private ViewPager mViewPager;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragment;

    private LinearLayout ll_top_safe;
    private LinearLayout ll_top_manage;
    private LinearLayout ll_top_tool;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        initEvent();
        setSelect(0);
    }

    /**
     * 获得布局
     */
    private void initView() {
        mViewPager = (ViewPager) findViewById(R.id.viewpager);

        ll_top_safe = (LinearLayout) findViewById(R.id.ll_top_safe);
        ll_top_manage = (LinearLayout) findViewById(R.id.ll_top_manage);
        ll_top_tool = (LinearLayout) findViewById(R.id.ll_top_tool);

        mFragment = new ArrayList<Fragment>();
        Fragment mTab1 = new SafeFragment();
        Fragment mTab2 = new ManageFragment();
        Fragment mTab3 = new ToolFragment();
        mFragment.add(mTab1);
        mFragment.add(mTab2);
        mFragment.add(mTab3);

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragment.get(position);
            }

            @Override
            public int getCount() {
                return mFragment.size();
            }
        };
        mViewPager.setAdapter(mAdapter);

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTab(mViewPager.getCurrentItem());
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 设置监听
     */
    private void initEvent() {
        ll_top_safe.setOnClickListener(this);
        ll_top_manage.setOnClickListener(this);
        ll_top_tool.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_top_safe:
                setSelect(0);
                break;
            case R.id.ll_top_manage:
                setSelect(1);
                break;
            case R.id.ll_top_tool:
                setSelect(2);
                break;
            default:
                break;
        }
    }

    private void resetImgs() {
        ll_top_safe.findViewById(R.id.iv_top_safe).setBackgroundColor(Color.parseColor("#3ba3d0"));
        ll_top_manage.findViewById(R.id.iv_top_manage).setBackgroundColor(Color.parseColor("#3ba3d0"));
        ll_top_tool.findViewById(R.id.iv_top_tool).setBackgroundColor(Color.parseColor("#3ba3d0"));
    }

    public void setSelect(int select) {
        setTab(select);
        mViewPager.setCurrentItem(select);
    }

    private void setTab(int select) {
        resetImgs();
        switch (select) {
            case 0:
                ll_top_safe.findViewById(R.id.iv_top_safe).setBackgroundColor(Color.parseColor("#ff2bd07a"));
                break;
            case 1:
                ll_top_manage.findViewById(R.id.iv_top_manage).setBackgroundColor(Color.parseColor("#ff2bd07a"));
                break;
            case 2:
                ll_top_tool.findViewById(R.id.iv_top_tool).setBackgroundColor(Color.parseColor("#ff2bd07a"));
                break;
        }
    }
}
